package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tbit.uqbike.object.pojo.UniAppMenu;
import com.tbit.uqbike.object.pojo.dto.UniAppMenusDTO;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.UniAppMenuService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序菜单模块
 *
 * @Author:BUGTian
 * @DATE: 2022/9/20
 */
@RestController
@RequestMapping("/uniAppMenu")
public class UniAppMenuController {
    @Autowired
    private UniAppMenuService uniAppMenuService;

    /**
     * 查询
     *
     * @param accountId 区域id
     * @param type      类型 1:首页，2:我的模块
     * @return
     */
    @GetMapping("/query")
    public Result<List<UniAppMenu>> query(Integer accountId, Integer type) {
        if (accountId == null || type == null) {
            return Result.error("参数有误");
        }
        List<UniAppMenu> list = uniAppMenuService.list(new QueryWrapper<UniAppMenu>().eq("accountId", accountId).eq("type", type).orderByAsc("sort"));
        list.forEach(UniAppMenu::initId);
        return Result.success(list);
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody UniAppMenusDTO uniAppMenusDTO) {
        List<UniAppMenu> data = uniAppMenusDTO.getData();
        if (CollectionUtil.isEmpty(data)) {
            return Result.error("参数有误");
        }
        return uniAppMenuService.edit(data);
    }

    /**
     * 新增
     *
     * @param uniAppMenu .
     * @return
     */
    @PostMapping("/put")
    public Result<BatchOperationMsgVO<String>> put(@RequestBody UniAppMenu uniAppMenu) {
        Assert.notNull(uniAppMenu.getSort(), "请填写sort排序");
        Assert.notNull(uniAppMenu.getName(), "请填写name菜单名称");
        Assert.notNull(uniAppMenu.getState(), "请填写state菜单状态");
        Assert.notNull(uniAppMenu.getIcon(), "请填写icon图标名称");
        Assert.notNull(uniAppMenu.getType(), "请选择type菜单防止页面 1:首页，2:我的模块");
        return uniAppMenuService.addMenuAndRearrange(uniAppMenu);
    }

}
